package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.NewProductBean;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.List;

/**
 * 新品展示
 */

public class NewProductAdapter extends SuperBaseAdapter<NewProductBean.DataBean> {
    private Context mContext;

    public NewProductAdapter(Context context, List<NewProductBean.DataBean> data) {
        super(context, data);
        this.mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder holder, NewProductBean.DataBean item, int position) {
        Glide.with(mContext).load(item.getImageUrl()).asBitmap().placeholder(R.mipmap.tuxiang_1).error(R.mipmap.tuxiang_1).into((ImageView) holder.getView(R.id.shangpin_image));
        holder.setText(R.id.shangpin_name, item.getCommodityName())
                .setText(R.id.shangping_price, "￥" + item.getNowPrice());
    }

    @Override
    protected int getItemViewLayoutId(int position, NewProductBean.DataBean item) {
        return R.layout.shangpin_item;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager manager = (GridLayoutManager) layoutManager;
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position - 1);
                    return (type == VIEW_TYPE.HEADER || type == VIEW_TYPE.FOOTER) ? manager.getSpanCount() : 1;
                }
            });
        }
    }
}
